<?php

namespace app\common\validate;

use app\lib\exception\ParameterException;

class OrderValidate extends BaseValidate
{

    protected $rule = [
        'products' => 'checkProducts',
    ];


    protected $singRule = [
        'product_id' => 'require|isNotEmpty',
        'count' => 'require|isPositiveInteger',
        'prduct_type' => 'require|checkprduct_type'
    ];

    /**
     * 检查商品
     */
    protected function checkProducts($values){
        if(!is_array($values)){
            throw new ParameterException([
                'msg' => '商品参数不合法'
            ]);
        }

        if(empty($values)){
            throw new ParameterException([
                'msg' => '商品列表不能为空'
            ]);
        }

        foreach ($values as $value){
            $this->checkProduct($value);
        }

        return true;
    }

    protected function checkProduct($value){
        $validate = new BaseValidate($this->singRule);
        $result = $validate->check($value);
        if(!$result){
            throw new ParameterException([
                'msg' => '商品列表参数错误'
            ]);
        }
    }

}